package com.lzh.exer;

import org.junit.Test;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author liuzhihao
 * @create 2021-02-25 9:36
 */
public class PicTest {

    //图片的加密
    @Test
    public void test1(){

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("1.jpg");
            fos = new FileOutputStream("5secret.jpg");
            byte[] buffer = new byte[20];
            int len;
            while( (len = fis.read(buffer)) !=-1){
                //字节数组进行修改
                //错误的：对数组本身没有修改
    //            for(byte b : buffer){
    //                b = (byte)(b ^ 5);
    //            }
                //正确的
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte)(buffer[i] ^5);
                }

                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    //图片的解密
    @Test
    public void test2(){

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("5secret.jpg");
            fos = new FileOutputStream("5.jpg");
            byte[] buffer = new byte[20];
            int len;
            while( (len = fis.read(buffer)) !=-1){
                //字节数组进行修改
                //错误的：对数组本身没有修改
                //            for(byte b : buffer){
                //                b = (byte)(b ^ 5);
                //            }
                //正确的
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte)(buffer[i] ^5);
                }

                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Test
    public void test3(){

        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            HashMap<Character,Integer> map = new HashMap<>();

            br = new BufferedReader(new FileReader("dbcp.txt"));
            bw = new BufferedWriter(new FileWriter("WorldCount.txt"));

            int c =0;
            while((c = br.read()) != -1){
                //int 还原为char
                char ch = (char)c;
                //判断char是否出现在map中第一次出现
                if(map.get(ch) == null){
                    map.put(ch,1);
                }else{
                    map.put(ch,map.get(ch) + 1);
                }
            }

            //遍历map，再写入数据
            Set<Map.Entry<Character, Integer>> entrySet = map.entrySet();
            for(Map.Entry<Character,Integer> entry : entrySet){
                switch (entry.getKey()){
                    case ' ':
                        bw.write("空格=" + entry.getValue());
                        break;
                    case '\t':
                        bw.write("tab键=" + entry.getValue());
                        break;
                    case '\r':
                        bw.write("回车=" + entry.getValue());
                        break;
                    case '\n':
                        bw.write("换行" + entry.getValue());
                        break;
                    default:
                        bw.write(entry.getKey() + "=" +entry.getValue());
                }
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
